﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace Comissoes.HtmlHelpers
{
    public static class Helpers
    {
        /// <summary>
        /// Cria uma label com classes de espaçamento
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="type">Tipo da classe | A - space | B - for10 |</param>
        /// <returns></returns>
        public static MvcHtmlString Space(this HtmlHelper helper, Char type)
        {
            TagBuilder tag = new TagBuilder("label");
            switch (type)
            {
                case 'A':
                    tag.AddCssClass("space");
                    break;
                case 'B':
                    tag.AddCssClass("for10");
                    break;
            }

            return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="id"></param>
        /// <param name="input"></param>
        /// <param name="controller"></param>
        /// <returns></returns>
        public static MvcHtmlString TextAutoComplete(this HtmlHelper helper, string id, string input, string controller)
        {
            TagBuilder tag = new TagBuilder("input");
            tag.Attributes.Add("name", input);
            tag.Attributes.Add("id", input);
            tag.Attributes.Add("type", "text");
            tag.Attributes.Add("onfocus", "fieldComplete($(this), '" + controller + "', '" + id + "');");

            return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="id"></param>
        /// <param name="input"></param>
        /// <param name="controller"></param>
        /// <returns></returns>
        public static MvcHtmlString TextAutoComplete(this HtmlHelper helper, string id, string input, string controller, object htmlAttr)
        {
            TagBuilder tag = new TagBuilder("input");
            tag.Attributes.Add("name", input);
            tag.Attributes.Add("id", input);
            tag.Attributes.Add("type", "text");
            tag.Attributes.Add("onfocus", "fieldComplete($(this), '" + controller + "', '" + id + "');");
            IDictionary<string, object> dict = new RouteValueDictionary(htmlAttr);
            foreach (var item in dict)
                tag.Attributes.Add(item.Key, item.Value.ToString());

            return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));
        }

        /// <summary>
        /// Monta um tag legend com o texto expecificado
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="text"></param>
        /// <returns></returns>
        public static MvcHtmlString Legend(this HtmlHelper helper, string text)
        {
            TagBuilder legend = new TagBuilder("legend");
            TagBuilder span = new TagBuilder("span");
            span.AddCssClass("ui-state-default ui-corner-all");
            span.SetInnerText(text);
            legend.InnerHtml = span.ToString(TagRenderMode.Normal);

            return MvcHtmlString.Create(legend.ToString(TagRenderMode.Normal));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="name"></param>
        /// <param name="classes"></param>
        /// <param name="method"></param>
        /// <param name="innerText"></param>
        /// <param name="disabled"></param>
        /// <param name="submit"></param>
        /// <returns></returns>
        public static MvcHtmlString Button(this HtmlHelper helper, string name, string classes, string method, string innerText, bool disabled, bool submit)
        {
            TagBuilder builder = new TagBuilder("button");
            builder.Attributes.Add("type", submit ? "submit" : "button");
            builder.AddCssClass(String.Format("{0} {1}", classes, name));
            if (disabled)
            {
                builder.Attributes.Add("disabled", "disabled");
            }
            builder.Attributes.Add("onclick", method + "()");
            builder.SetInnerText(innerText);
            return MvcHtmlString.Create(builder.ToString(TagRenderMode.Normal));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="name"></param>
        /// <param name="classes"></param>
        /// <param name="method"></param>
        /// <param name="innerText"></param>
        /// <param name="disabled"></param>
        /// <returns></returns>
        public static MvcHtmlString Button(this HtmlHelper helper, string name, string classes, string method, string innerText, bool disabled)
        {
            return Button(helper, name, classes, method, innerText, disabled, false);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="name"></param>
        /// <param name="classes"></param>
        /// <param name="method"></param>
        /// <param name="innerText"></param>
        /// <returns></returns>
        public static MvcHtmlString Button(this HtmlHelper helper, string name, string classes, string method, string innerText)
        {
            return Button(helper, name, classes, method, innerText, false, false);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="name"></param>
        /// <param name="classes"></param>
        /// <param name="method"></param>
        /// <param name="innerText"></param>
        /// <param name="disabled"></param>
        /// <returns></returns>
        public static MvcHtmlString SubmitButton(this HtmlHelper helper, string name, string classes, string method, string innerText, bool disabled)
        {
            return Button(helper, name, classes, method, innerText, disabled, true);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="name"></param>
        /// <param name="classes"></param>
        /// <param name="method"></param>
        /// <param name="innerText"></param>
        /// <returns></returns>
        public static MvcHtmlString SubmitButton(this HtmlHelper helper, string name, string classes, string method, string innerText)
        {
            return Button(helper, name, classes, method, innerText, false, true);
        }

        /// <summary>
        /// Monta a estrutura padrão para grids
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="table"></param>
        /// <param name="pager"></param>
        /// <returns></returns>
        public static MvcHtmlString jqGrid(this HtmlHelper helper)
        {
            TagBuilder tFieldset = new TagBuilder("fieldset");
            TagBuilder tTable = new TagBuilder("table");
            TagBuilder tPager = new TagBuilder("div");

            tFieldset.AddCssClass("container transparent");
            tTable.Attributes.Add("id", "table");
            tPager.Attributes.Add("id", "pager");

            tFieldset.InnerHtml = tTable.ToString(TagRenderMode.Normal) + tPager.ToString(TagRenderMode.Normal);

            return MvcHtmlString.Create(tFieldset.ToString(TagRenderMode.Normal));
        }

        /// <summary>
        /// Monta a estrutura padrão para grids
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="table"></param>
        /// <param name="pager"></param>
        /// <returns></returns>
        public static MvcHtmlString jqGrid(this HtmlHelper helper, string table, string pager)
        {
            TagBuilder tFieldset = new TagBuilder("fieldset");
            TagBuilder tTable = new TagBuilder("table");
            TagBuilder tPager = new TagBuilder("div");

            tFieldset.AddCssClass("container transparent");
            tTable.Attributes.Add("id", table);
            tPager.Attributes.Add("id", pager);

            tFieldset.InnerHtml = tTable.ToString(TagRenderMode.Normal) + tPager.ToString(TagRenderMode.Normal);

            return MvcHtmlString.Create(tFieldset.ToString(TagRenderMode.Normal));
        }

        /// <summary>
        /// Monta a estrutura padrão para grids
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="table"></param>
        /// <param name="pager"></param>
        /// <param name="classes"></param>
        /// <returns></returns>
        public static MvcHtmlString jqGrid(this HtmlHelper helper, string table, string pager, string classes)
        {
            TagBuilder tFieldset = new TagBuilder("fieldset");
            TagBuilder tTable = new TagBuilder("table");
            TagBuilder tPager= new TagBuilder("div");

            tFieldset.AddCssClass("container transparent " + classes);
            tTable.Attributes.Add("id", table);
            tPager.Attributes.Add("id", pager);

            tFieldset.InnerHtml = tTable.ToString(TagRenderMode.SelfClosing) + tPager.ToString(TagRenderMode.SelfClosing);

            return MvcHtmlString.Create(tFieldset.ToString(TagRenderMode.Normal));
        }

        /// <summary>
        /// Cria uma tag label
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="classes"></param>
        /// <param name="innerText"></param>
        /// <returns></returns>
        public static MvcHtmlString Label(this HtmlHelper helper, string classes, string innerText)
        {
            TagBuilder builder = new TagBuilder("label");
            builder.AddCssClass(classes + " ui-widget");
            builder.SetInnerText(innerText);

            return MvcHtmlString.Create(builder.ToString(TagRenderMode.Normal));
        }

        /// <summary>
        /// Cria uma tag label com for
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="labelFor"></param>
        /// <param name="classes"></param>
        /// <param name="innerText"></param>
        /// <returns></returns>
        public static MvcHtmlString Label(this HtmlHelper helper, string labelFor, string classes, string innerText)
        {
            TagBuilder builder = new TagBuilder("label");
            builder.Attributes.Add("for", labelFor);
            builder.AddCssClass(classes + " ui-widget");
            builder.SetInnerText(innerText);

            return MvcHtmlString.Create(builder.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString Lookup(this HtmlHelper helper, string url, string title, string codigo, string descricao, string tipo, Boolean triggerChange)
        {
            TagBuilder label = new TagBuilder("label");
            label.AddCssClass("lookup lookup" + title);
            TagBuilder image = new TagBuilder("img");
            image.Attributes.Add("alt", "busca");
            image.Attributes.Add("title", "localizar " + title);
            image.Attributes.Add("onclick", "javascript:loadLookup('" + url + "', 'Localizar " + title + "', '" + codigo + "', '" + descricao + "', '" + tipo + "', " + triggerChange.ToString().ToLower() + ")");
            image.Attributes.Add("src", "../../Content/Images/search.png");
            image.AddCssClass("lookup");

            label.InnerHtml = image.ToString(TagRenderMode.SelfClosing);

            return MvcHtmlString.Create(label.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString Lookup(this HtmlHelper helper, string url, string title, string codigo, string descricao, string tipo)
        {
            return Lookup(helper, url, title, codigo, descricao, tipo, false);
        }

        public static MvcHtmlString LabelInput(this HtmlHelper helper, MvcHtmlString input, string classes)
        {
            TagBuilder builder = new TagBuilder("label");
            builder.AddCssClass(classes);
            builder.InnerHtml = input.ToHtmlString();
            return MvcHtmlString.Create(builder.ToString(TagRenderMode.Normal));
        }
    }
}